package BandB.Tool.GPhoneManager.TaskManager;

import java.util.ArrayList;

import BandB.Tool.GPhoneManager.TaskManager.Util.DataHandler;
import BandB.Tool.GPhoneManager.TaskManager.Util.Utilities;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class NotificationService extends Service {

	private static final String TAG = "NotificationService";
	private DataHandler dataHandler;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		Log.e(TAG, "NotificationService start");
		dataHandler = new DataHandler(this);
		ArrayList<ApplicationInfo> apps = Utilities.getRunningAppList(this);
		String currentPackage = getPackageName();
		// Toast.makeText(context, "Broadcast Receive", Toast.LENGTH_LONG)
		// .show();
		for (int i = 0; i < apps.size(); i++) {
			if (apps.get(i).appInfo.packageName.equals(currentPackage)) {
				continue;
			}
			try {
				if (dataHandler
						.IsSystemProcess(apps.get(i).appInfo.packageName)) {
					continue;
				}
				ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
				am.restartPackage(apps.get(i).appInfo.packageName);
			} catch (Exception e) {

				Log.e("NotificationService", "killProcess error:"
						+ e.toString(), e);
			}
		}
		try {
			ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
			am.restartPackage(currentPackage);
		} catch (Exception e) {
			// TODO: handle exception
		}

		super.onStart(intent, startId);
	}
}
